﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YueRu.Common
{
    public static class Conv
    {
        #region 数值转换
        
        #region ToInt
        /// <summary>
        /// 字符串转换为int32
        /// </summary>
        /// <param name="objInt">待转换的字符</param>
        /// <param name="defaultValue">默认值(转换失败设置的值)</param>
        /// <returns></returns>
        public static int ToInt(object objInt, int defaultValue)
        {
            if (objInt == null)
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(objInt);
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 字符串转换为int32
        /// </summary>
        /// <param name="objInt">待转换的字符(转换失败默认返回0)</param>
        /// <returns></returns>
        public static int ToInt(object objInt)
        {
            return ToInt(objInt, 0);
        }

        #endregion ToInt

        #region ToInt64
        /// <summary>
        /// 转换为int64
        /// </summary>
        /// <param name="objInt">待转换的字符</param>
        /// <param name="defaultValue">默认值(转换失败设置的值)</param>
        /// <returns></returns>
        public static long ToInt64(object objInt, long defaultValue)
        {
            if (objInt == null)
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return Convert.ToInt64(objInt);
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 字符串转换为int64
        /// </summary>
        /// <param name="objInt">待转换的字符(转换失败默认返回0)</param>
        /// <returns></returns>
        public static long ToInt64(object objInt)
        {
            return ToInt64(objInt, 0);
        }
        #endregion ToInt

        #region ToLong
        /// <summary>
        /// 字符串转换为long
        /// </summary>
        /// <param name="objLong">待转换的字符(转换失败默认返回0)</param>
        /// <param name="defaultValue">默认值(转换失败设置的值)</param>
        /// <returns></returns>
        public static long ToLong(object objLong, long defaultValue)
        {
            if (objLong == null)
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return long.Parse(objLong.ToString());
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 字符串转换为long
        /// </summary>
        /// <param name="objLong">待转换的字符(转换失败默认返回0)</param>
        /// <returns></returns>
        public static long ToLong(object objLong)
        {
            return ToLong(objLong, 0);
        }
        #endregion ToBoolean

        #region ToBoolean
        /// <summary>
        /// 转换为Boolean
        /// </summary>
        /// <param name="objBool">待转换的字符(转换失败默认返回false)</param>
        /// <param name="defaultValue">默认值(转换失败设置的值)</param>
        /// <returns></returns>
        public static bool ToBoolean(object objBool, bool defaultValue)
        {
            if (objBool == null)
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return Convert.ToBoolean(objBool);
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 转换为Boolean
        /// </summary>
        /// <param name="objBool">待转换的字符(转换失败默认返回false)</param>
        /// <returns></returns>
        public static bool ToBoolean(object objBool)
        {
            return ToBoolean(objBool, false);
        }
        #endregion ToBoolean

        #region ToDecimal
        /// <summary>
        /// 转换为decimal
        /// </summary>
        /// <param name="objdecimal">待转换的字符(转换失败默认返回0)</param>
        /// <param name="defaultValue">默认值(转换失败设置的值)</param>
        /// <returns></returns>
        public static decimal ToDecimal(object objdecimal, decimal defaultValue)
        {
            if (objdecimal == null)
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return Convert.ToDecimal(objdecimal);
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 转换为decimal
        /// </summary>
        /// <param name="objdecimal">待转换的字符(转换失败默认返回0)</param>
        /// <returns></returns>
        public static decimal ToDecimal(object objdecimal)
        {
            return ToDecimal(objdecimal, 0);
        }
        #endregion ToDecimal

        #region ToFloat
        /// <summary>
        /// 转换为float
        /// </summary>
        /// <param name="objFloat">待转换的字符(转换失败默认返回0)</param>
        /// <param name="defaultValue">默认值(转换失败设置的值)</param>
        /// <returns></returns>
        public static float ToFloat(object objFloat, float defaultValue)
        {
            if (objFloat == null)
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return Convert.ToSingle(objFloat);
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 转换为float
        /// </summary>
        /// <param name="objFloat">待转换的字符(转换失败默认返回0)</param>
        /// <returns></returns>
        public static float ToFloat(object objFloat)
        {
            return ToFloat(objFloat, 0);
        }
        #endregion ToFloat

        #endregion
    }
}
